home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / wink / source / wc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  6.8 KB  |  317 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <ctype.h>
  4. #include    <strings.h>
  5. #include    <msdos.cf>
  6.  
  7. #define SHORT   short int
  8. #define LONG    long int
  9. #define ULONG   unsigned long int
  10. #define BOOL    int
  11.  
  12. #define TRUE    1
  13. #define FALSE   0
  14. #define ERR     (-1)
  15.  
  16. #define    MAXWCP    300
  17.  
  18. typedef struct {
  19.     SHORT   wb_dsp;
  20.     SHORT   wb_att;
  21.     SHORT   wb_time,wb_date;
  22.     ULONG   wb_size;
  23.     char    wb_name[14];
  24. } WCBUF;
  25.  
  26. extern void    wrtstr();
  27. extern void    Dmy_form();
  28. extern int    getch();
  29. extern void    Dsp_vram_flash();
  30.  
  31. static int    wcmax=0;
  32. static WCBUF    *topwcp=NULL;
  33. static char    dir[80];
  34. static char     wild[80];
  35.  
  36. char    *endname(char *name)
  37. {
  38.     char    *p;
  39.  
  40.     for ( p=name ; *p != '\0' ; p++ );
  41.     for ( ; p != name && *p != '\\' && *p != ':' ; p-- );
  42.     if ( p != name )
  43.         p++;
  44.     return p;
  45. }
  46. char    *subname(char *name)
  47. {
  48.     static char tmp[14];
  49.     int        i;
  50.     char    *p;
  51.  
  52.     if ( *name == '.' ) {
  53.     for ( i = 0,p = tmp ; i < 9 ; i++ ) {
  54.         if ( *name == '\0' )
  55.         *(p++) = ' ';
  56.         else
  57.         *(p++) = *(name++);
  58.     }
  59.     } else {
  60.     for ( i = 0,p = tmp ; i < 9 ; i++ ) {
  61.         if ( *name == '.' || *name == '\0' )
  62.         *(p++) = ' ';
  63.         else
  64.         *(p++) = *(name++);
  65.     }
  66.     if ( *name == '.' ) name++;
  67.     }
  68.     for ( ; i < 12 ; i++ ) {
  69.     if ( *name == '\0' )
  70.         *(p++) = ' ';
  71.     else
  72.         *(p++) = *(name++);
  73.     }
  74.     *p = '\0';
  75.     return tmp;    
  76. }
  77. int    cmpname(char *arg,char *str)
  78. {
  79.     int     i;
  80.     char    *p,tmp[14];
  81.  
  82.     strcpy(tmp,subname(arg));
  83.     p = subname(str);
  84.     if ( (i = strcmp(tmp + 9,p + 9)) != 0 )
  85.     return i;
  86.     tmp[9] = p[9] = '\0';
  87.     return strcmp(tmp,p);
  88. }
  89. void    sort(int bs,int mx)
  90. {
  91.     int     i,j;
  92.     WCBUF   x,w;
  93.  
  94.     i=bs;
  95.     j=mx;
  96.     memcpy(&x,&(topwcp[bs + (mx - bs) / 2]),sizeof(WCBUF));
  97.     do {
  98.         while ( cmpname(topwcp[i].wb_name, x.wb_name) < 0 )
  99.             i++;
  100.         while ( cmpname(topwcp[j].wb_name, x.wb_name) > 0 )
  101.             j--;
  102.         if ( i <= j ) {
  103.             memcpy(&w,&(topwcp[i]),sizeof(WCBUF));
  104.             memcpy(&(topwcp[i]),&(topwcp[j]),sizeof(WCBUF));
  105.             memcpy(&(topwcp[j]),&w,sizeof(WCBUF));
  106.             i++; j--;
  107.         }
  108.     } while ( i <= j );
  109.     if ( bs < j )
  110.         sort(bs,j);
  111.     if ( i < mx )
  112.         sort(i,mx);
  113. }
  114. BOOL    subwc(char *arg,int mode,WCBUF *wcp)
  115. {
  116.     static BOOL opflg=FALSE;
  117.     static struct {
  118.         char    dd_dmy[22];
  119.         SHORT   dd_time,dd_date;
  120.         LONG    dd_size;
  121.         char    dd_name[13];
  122.     } dma;
  123.  
  124.     if ( opflg == FALSE ) {
  125.         Registers.AX.R = 0x1A00;
  126.         Registers.DX.R = (int)&dma;
  127.         Registers.DS.R = getds();
  128.         calldos(); 
  129.         Registers.AX.R = 0x4E00;
  130.         Registers.CX.R = mode;
  131.         Registers.DX.R = (int)arg;
  132.         Registers.DS.R = getds();
  133.         opflg = TRUE;
  134.     }
  135.     else
  136.         Registers.AX.R = 0x4F00;
  137.  
  138.     calldos();
  139.     if ( (Registers.Flags & 0x0001) != 0 ) {
  140.         opflg = FALSE;
  141.     return FALSE;
  142.     }
  143.  
  144.     wcp->wb_dsp = 0;
  145.     wcp->wb_att = dma.dd_dmy[21];
  146.     wcp->wb_date = dma.dd_date;
  147.     wcp->wb_time = dma.dd_time;
  148.     wcp->wb_size = dma.dd_size;
  149.     strcpy(wcp->wb_name,dma.dd_name);
  150.     return TRUE;
  151. }
  152. void    chdir(char *name)
  153. {
  154.     Registers.AX.R = 0x3B00;
  155.     Registers.DX.R = (int)name;
  156.     Registers.DS.R = getds();
  157.     calldos();
  158.     if ( name[1] == ':' ) {
  159.     Registers.AX.R = 0x0E00;
  160.     Registers.DX.R = (*name & 0xDF) - 'A';
  161.     calldos();
  162.     }
  163. }
  164. int    retdir(char *name)
  165. {
  166.     Registers.AX.R = 0x4700;
  167.     Registers.DX.R = 0x0000;
  168.     Registers.SI.R = (int)name+3;
  169.     Registers.DS.R = getds();
  170.     calldos();
  171.  
  172.     Registers.AX.R = 0x1900;
  173.     calldos();
  174.  
  175.     *(name++) = 'A' + (Registers.AX.R & 0xFF);
  176.     *(name++) = ':';
  177.     *(name++) = '\\';
  178.     if ( *name != '\0' )
  179.     strcat(name,"\\");
  180.     return (Registers.AX.R & 0xFF);
  181. }
  182. int    WC_open(char *file)
  183. {
  184.     int     i;
  185.     char    *p,*s;
  186.  
  187.     if ( (s = endname(file)) != file ) {
  188.     for ( p = dir ; file != s ; )
  189.         *(p++) = *(file++);
  190.     if ( *(p-1) == '\\' ) p--;
  191.     *p = '\0';
  192.     chdir(dir);
  193.     }
  194.     strcpy(wild,s);
  195.  
  196.     if ( topwcp == NULL &&
  197.     (topwcp = (WCBUF *)malloc(sizeof(WCBUF)*MAXWCP)) == NULL )
  198.     return 0;
  199.     wcmax = 0;
  200.     while ( wcmax < MAXWCP && subwc("*.*",0x10,&topwcp[wcmax]) != FALSE ) {
  201.     if ( (topwcp[wcmax].wb_att & 0x10) != 0 )
  202.         wcmax++;
  203.     }
  204.     if ( wcmax > 1 ) sort(0,wcmax - 1);
  205.     i = wcmax;
  206.     while ( wcmax < MAXWCP && subwc(wild,0x21,&topwcp[wcmax]) != FALSE )
  207.     wcmax++;
  208.     if ( (wcmax - 1) > i ) sort(i,wcmax - 1);
  209.  
  210.     retdir(dir);
  211.     return wcmax;
  212. }
  213. void    File_dsp(off,no)
  214. int    off,no;
  215. {
  216.     int     i,c;
  217.     char    *p;
  218.  
  219.     for ( i = 0 ; i < 30 ; i++ ) {
  220.     c = 0x05;
  221.     if ( (i+off) >= wcmax )
  222.         p = "            ";
  223.     else {
  224.         p = subname(topwcp[i+off].wb_name);
  225.         if ( (topwcp[i+off].wb_att & 0x10) != 0 )
  226.             c = 0x0D;
  227.         if ( (i+off) == no )
  228.         c |= 0x10;
  229.     }
  230.     wrtstr(p,17+(i%3)*14,11+(i/3),c);
  231.     }
  232. }
  233. int    File_sel(char *file)
  234. {
  235.     int     i,no,off,fg,ch;
  236.     int        no_s,off_s;
  237.     char    tmp[160];
  238.     char    *p;
  239.     char    bkdir[80];
  240.  
  241.     if ( *file == '\0' )
  242.     strcpy(file,"*.*");
  243.  
  244.     for ( p = file ; *p != '*' && *p != '?' && *p != '\0' ; p++ );
  245.     if ( *p == '\0' )
  246.     return FALSE;
  247.  
  248.     retdir(bkdir);
  249.  
  250.     if ( WC_open(file) == 0 )
  251.     return FALSE;
  252.  
  253.     Dmy_form(tmp,44,0x98,0x95,0x99); 
  254.     wrtstr(tmp,15,8,0x05);
  255.  
  256.     Dmy_form(tmp,44,0x96,0x20,0x96); 
  257.     for ( i = 0 ; i < 12 ; i++ )
  258.         wrtstr(tmp,15,9+i,0x05);
  259.  
  260.     Dmy_form(tmp,44,0x9A,0x95,0x9B); 
  261.     wrtstr(tmp,15,21,0x05);
  262.  
  263.     Dmy_form(tmp,44,0x93,0x95,0x92); 
  264.     wrtstr(tmp,15,10,0x05);
  265.  
  266.     Dmy_form(tmp,44,0x96,0x20,0x96); 
  267.     wrtstr(dir,17,9,0x05);
  268.  
  269.     for ( no_s = off_s = 1,no = off = 0,fg = FALSE ; fg == FALSE ; ) { 
  270.     if ( no != no_s || off != off_s ) {
  271.             File_dsp(off,no);
  272.         no_s = no; off_s = off;
  273.     }
  274.     ch = getch();
  275.     if ( ch == '\x0D' ) {
  276.         if ( (topwcp[no].wb_att & 0x10) != 0 ) {
  277.         strcat(dir,topwcp[no].wb_name);
  278.         strcat(dir,"\\");
  279.         strcat(dir,wild);
  280.         WC_open(dir);
  281.                 wrtstr(tmp,15,9,0x05);
  282.                 wrtstr(dir,17,9,0x05);
  283.         no_s = off_s = 1;
  284.         no = off = 0;
  285.         } else {
  286.         fg = TRUE;
  287.         }
  288.     } else if ( ch == '\x1B' ) {
  289.         fg = ERR;
  290.     } else if ( ch == '\x1C' && no < (wcmax-1) ) {
  291.         no++;
  292.     } else if ( ch == '\x1D' && no > 0 ) {
  293.         no--;
  294.     } else if ( ch == '\x1F' && (no + 3) < wcmax ) {
  295.         no += 3;
  296.     } else if ( ch == '\x1E' && no >= 3 ) {
  297.         no -= 3;
  298.     }
  299.     while ( no < off ) off -= 3;
  300.     while ( no >= (off+30) ) off += 3;
  301.     }
  302.     Dsp_vram_flash();
  303.     if ( fg == TRUE ) {
  304.     strcat(dir,topwcp[no].wb_name);
  305.     strcpy(file,dir);
  306.     fg = FALSE;
  307.     }
  308.     free(topwcp);
  309.     topwcp = NULL;
  310.  
  311.     if ( bkdir[3] != '\0' )
  312.     bkdir[strlen(bkdir)-1] = '\0';
  313.     chdir(bkdir);
  314.  
  315.     return fg;
  316. }
  317.